{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于slim框架的卷积神经网络训练\n",
    "\n",
    "给出代码运行的log截图，并提供文档描述对整个模型训练过程的理解。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码不作为评判标准，如果运行正确，则认为代码没有错误。\n",
    "\n",
    "### - log输出没有明显的报错，Top1(Accuracy)不低于60%，Top5(Recall)不低于70`分。\n",
    "### - Top5召回率达到90%以上 10分。\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![30000](hw11-images/30000.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![evaluation](hw11-images/evaluation_emphasis.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为 batch_size 设成 32 会出现 [Resource exhausted: OOM when allocating tensor…] 错误讯息，所以将 batch_size 改设为 **16**，max_number_of_steps 也随之从 15000 调整到 **30000**，使**训练的 epoch 数保持不变**（32\\*15000=16\\*30000）。  \n",
    "\n",
    "机器上面有两张**NVIDIA GeForce GTX 965M**显卡，但**每张的显存只有2GB**，所以会出现上述的异常错误导致训练根本无法开始就自动结束。将 batch_size 改设为 16 后，30000 次训练可顺利跑完，前后共花费约**七个小时**（中间碰到 Windows 更新自动重开机），感谢老师的代码有使用 save 与 restore，可以从 checkpoint 中读取训练中断前最接近几次的配置与变量，接续执行训练。\n",
    "\n",
    "**Top1(Accuracy) = 0.904687524** > 60%  \n",
    "**Top5(Recall) = 0.974583328** > 70%  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对模型训练的整个流程的理解和描述 20分。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 首先要下载训练集和测试集的数据: train 和 test2，下载 tensorflow 提供的**预训练**的 inception_V3 模型参数, 经过两次解压缩后会得到 inception_V3.ckpt。\n",
    "\n",
    "2. 使用 tf 官网下 slim 框架自带的 download_and_convert_data.py 脚本对数据集进行预处理，生成训练集和验证集的 **tfrecord 格式文件**，以及**保存标签的 label.txt 纯文字文件**: 共有八个二进制 tfrecord 格式文件:   \n",
    "\n",
    "    - quiz_train_00000-of-00004.tfrecord,\n",
    "    - quiz_train_00001-of-00004.tfrecord，\n",
    "    - quiz_train_00002-of-00004.tfrecord，\n",
    "    - quiz_train_00002-of-00004.tfrecord，\n",
    "    - quiz_validation_00000-of-00004.tfrecord，\n",
    "    - quiz_validation_00001-of-00004.tfrecord，\n",
    "    - quiz_validation_00002-of-00004.tfrecord，\n",
    "    - quiz_validation_00003-of-00004.tfrecord。    \n",
    "  \n",
    "  \n",
    "3. 使用 train_image_classifier.py 脚本对训练数据集进行 inception_V3 模型训练, 一次训练（step）读入 16 张图（称为 batch_size）, 为了匹配训练集的实际情况，**除去原模型 logits 和 AuxLogits 部分**，**初始学习率订为 0.001**。tf 会依据程序执行的情况不断产生类似 model.ckpt-27465.data-00000-of-00001, model.ckpt-30000.index, model.ckpt-30000.meta 名称的纪录文件，其中 27465 代表执行到第 27465 次（step）。\n",
    "\n",
    "4. 使用 eval_image_classifier.py 脚本对验证数据集进行 inception_V3 模型验证, 一次验证（step）也读入 16 张图, 执行完会得到两个统计数据 (Top1(Accuracy) 和 Top5(Recall))，可以**视为到目前为止训练的成果**。\n",
    "\n",
    "5. 验证完后使用 export_inference_graph.py 和 freeze_graph.py **导出模型与训练的最终参数**, 得到 inception_v3_inf_graph.pb 和 freezed.pb 两个文件，分别用于**存放标签和带有参数的计算图**。\n",
    "\n",
    "6. 输入或上传一张图片对其结果进行测试(有略为修改 server.py，否则在 Windows 环境下无法显示上传的图片，只能看到判断的概率)。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![test_1](hw11-images/test_1.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![test_1](hw11-images/test_8.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### tensorboard 图标的解读 10分。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SCALARS/losses (损失函数曲线)：可见训练是有效的，损失一直在下降，在 22000 step 后开始有些振荡，并没有走平，确实可以尝试继续训练以进一步优化模型。\n",
    "\n",
    "![softmax_cross_entropy_loss](hw11-images/softmax_cross_entropy_loss.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SCALARS/sparsity (计算图节点处权值的稀疏度): 较好的稀疏度可以保证模型不会过拟合，也提高了模型的泛化能力。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![sparsity.PNG](hw11-images/sparsity.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SCALARS/total_loss：这就是加上正则损失后的总损失, 也在 22000 step 后开始有些振荡，并没有走平。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![total_loss](hw11-images/total_loss.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IMAGES：显示对输入图像的放大，颠倒及裁切结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![images](hw11-images/images.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GRAPHS: 整个模型的结构组织。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![graphs](hw11-images/graphs.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DISTRIBUTIONS：计算图每个节点处权重的分布。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![distributions](hw11-images/distributions.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "HISTOGRAMS: 每一层权重参数变化情况。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![histograms](hw11-images/histograms.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
